package servlet;


import bean.wareHouse;
import com.github.pagehelper.PageInfo;
import dao.impl.wareHouseImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/wareHouseServlet")
public class wareHouseServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //防止中文乱码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取页面传来的数据
        String method=request.getParameter("method");

        //增加
        if(method.equals("wareHouseAdd")){
            String whName = request.getParameter("wareHouseName");
            int whCap = Integer.parseInt(request.getParameter("wareHouseCap"));

            int insertWareHouse = new wareHouseImpl().wareHouseAdd(whName,whCap);
            if(insertWareHouse==1){
                //重新加载 重定向
                response.sendRedirect("wareHouseServlet?method=wareHouseSelect");
            }
        }else if (method.equals("wareHouseDel")){
            int delId = Integer.parseInt(request.getParameter("whId"));
            int delWareHouse = new wareHouseImpl().wareHouseDel(delId);
            if(delWareHouse==1){
                response.sendRedirect("wareHouseServlet?method=wareHouseSelect");
            }
        }else if (method.equals("wareHouseSignal")){
            int whId = Integer.parseInt(request.getParameter("whId"));
            wareHouse whSignl= new wareHouseImpl().wareHouseSingal(whId);
            request.setAttribute("whSignl",whSignl);
            request.getRequestDispatcher("/wareHouseUpdate.jsp").forward(request,response);

        }else if (method.equals("wareHouseOne")){
            String whName = request.getParameter("whName");
            List<wareHouse>  whOne= new wareHouseImpl().wareHouseOne(whName);
            request.setAttribute("whList",whOne);
            request.getRequestDispatcher("/wareHouseSelect.jsp").forward(request,response);

        }else if (method.equals("wareHouseUpdate")){

            int whId = Integer.parseInt(request.getParameter("whId"));
            String whName = request.getParameter("wareHouseName");
            int whCap = Integer.parseInt(request.getParameter("wareHouseCap"));
            int upaWareHouse = new wareHouseImpl().wareHouseChange(whId,whName,whCap);
            if(upaWareHouse==1){
                response.sendRedirect("wareHouseServlet?method=wareHouseSelect");
            }
        }else if (method.equals("wareHouseSelect")){
            List<wareHouse> list = new wareHouseImpl().whList();
            request.setAttribute("whList",list);

            request.getRequestDispatcher("/wareHouseSelect.jsp").forward(request,response);

        }


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
